package com.desire.homework;

import java.util.Random;

/**
 * @author desire
 */
public class _21_生成随机数字验证码 {
    public static void main(String[] args) {
        String s = generateNumCaptcha(6);
        System.out.println(s);
        String s1 = generateNumCaptcha();
        System.out.println(s1);

        String ss = generateStrCaptcha(6);
        System.out.println(ss);
        String ss1 = generateStrCaptcha();
        System.out.println(ss1);
    }

    public static String generateStrCaptcha() {
        return generateStrCaptcha(4);
    }

    public static String generateStrCaptcha(int digits) {
        Random random = new Random();
        String str = "0123456789abcdefghijklmnolpqrstuvwxyzABCDEFGHIJKLMNOLPQRSTUVWXYZ";
        StringBuilder sbu = new StringBuilder();
        for (int i = 0; i < digits; i++) {
            sbu.append(str.charAt(random.nextInt(str.length())));
        }
        return sbu.toString();
    }


    public static String generateNumCaptcha() {
        return generateNumCaptcha(4);
    }

    public static String generateNumCaptcha(int digits) {
        Random random = new Random();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < digits; i++) {
            int captcha = random.nextInt(0, 10);
            stringBuilder.append(captcha);
        }
        return stringBuilder.toString();
    }
}
